package com.wy.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.nio.charset.StandardCharsets;

public class EncodingWrapper extends HttpServletRequestWrapper {
    private HttpServletRequest request;
    private String encoding;
    public EncodingWrapper(HttpServletRequest request, String encoding) {
        super(request);
        this.request = request;
        if (encoding == null) {
            encoding = DataConst.DEFAULT_CHARSET;
        }
        this.encoding = encoding;
    }

    @Override
    public String getParameter(String name) {
        String value = request.getParameter(name);
        if (value == null) {
            return null;
        }
        //使用ISO-8859-1进行编码，通过UTF-8进行解码
        try {
            value = new String(value.getBytes(StandardCharsets.ISO_8859_1), encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
